package com.example.revise.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.revise.R
import com.example.revise.data.VideoItem
import com.example.revise.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    private var currentPosition = 0
    private var videoAdapter: VideoAdapter? = null

    companion object {
        // 使用静态列表以便其他Fragment可以访问相同的数据
        val videos = listOf(
            VideoItem(R.raw.number_202501231201, "不能去丙科室", "分步分组"),
            VideoItem(R.raw.number_202501231202, "不相邻", "插空法"),
            VideoItem(R.raw.number_202501231203, "排列数表格", "多背几次", true),
            VideoItem(R.raw.number_202501231204, "单打双打", "鸡兔同笼"),
            VideoItem(R.raw.number_202501231205, "组成三角形", "反向思考"),
            VideoItem(R.raw.number_202501231206, "组合数表格", "多背几次", true)
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {
            // 创建适配器
            videoAdapter = VideoAdapter(videos)

            // 设置 ViewPager2
            binding.viewPager.apply {
                adapter = videoAdapter
                orientation = ViewPager2.ORIENTATION_VERTICAL

                registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        super.onPageSelected(position)
                        try {
                            videoAdapter?.pauseVideo(currentPosition)
                            videoAdapter?.playVideo(position)
                            currentPosition = position
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                })
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onDestroyView() {
        try {
            binding.viewPager.adapter = null
            videoAdapter = null
        } catch (e: Exception) {
            e.printStackTrace()
        }
        _binding = null
        super.onDestroyView()
    }

    override fun onPause() {
        super.onPause()
        try {
            videoAdapter?.pauseVideo(currentPosition)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()
        try {
            videoAdapter?.playVideo(currentPosition)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}